/**
 * 
 */
package fb.chatter.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;

/**
 * @author hoannguyen
 * 
 */
public final class HttpConnectionUtils {

	/**
	 * Build and send a GET request with the given URL.
	 * 
	 * @param strUrl
	 * @return the response text body.
	 */
	public static final String doGetTextRequest(String strUrl) {

		String responseContent = null;

		try {
			URL url = new URL(strUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);
			connection.connect();

			InputStream is = connection.getInputStream();
			responseContent = IOUtils.toString(is, "UTF-8");
		} catch (IOException e) {
			ExceptionUtils.log(e);
		}

		return responseContent;
	}
	
	/**
	 * Build and send a GET request with the given URL.
	 * 
	 * @param strUrl
	 * @return the response text body.
	 */
	public static final InputStream doGetInputStreamRequest(String strUrl) {
		
		InputStream is = null;
		
		try {
			URL url = new URL(strUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);
			connection.connect();
			
			is = connection.getInputStream();
		} catch (IOException e) {
			ExceptionUtils.log(e);
		}
		
		return is;
	}
}
